import os
import glob as glob
import pdb
import json

phone_to_id = {}

def phone_all():
    input_dir = '/opt/tiger/arnold_test/workstation/data/single_speaker/data_biaobei/'
    phone_to_id_json_file = 'biaobei_phone_to_id_dict.json'

    phone_files = glob.glob(os.path.join(input_dir,'PhoneLabeling','*.interval'))
    phone_files.sort()

    index = 1

    for phone_file in phone_files:
        phone_list = []
        with open(phone_file,'r') as phone_file_handle:
            for i in range(12):
                phone_file_handle.readline()
            start_line = phone_file_handle.readline().strip('\n')
            while start_line:
                end_line = phone_file_handle.readline().strip('\n')
                phone = phone_file_handle.readline().strip('\n')
                phone = phone.replace('\"','')
                if phone not in phone_to_id.keys():
                    phone_to_id[phone] = index
                    index += 1
                start_line = phone_file_handle.readline().strip('\n')
    
    phone_to_id['UNK'] = index
    index += 1
    phone_to_id['EOS'] = index
    with open(phone_to_id_json_file,'w+') as f:
        json.dump(phone_to_id,f)

if __name__ == "__main__":
    phone_all()